namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class IndicatorSimpleMovingAverage : BaseIndicator
    {
        private LineShape tlsSma;

        public IndicatorSimpleMovingAverage()
        {
            base.IndType = IndicatorType.SMA;
            base.IndName = "SMA";
            base.IndDesc = "平滑移动平均线";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 5.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "M", 1.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "SMA" };
        }

        public override bool Contains(object obj)
        {
            return (obj == this.tlsSma);
        }

        public override double[] Get(int r)
        {
            DataRow row1 = base.Crab.DataSource.Rows[r];
            double curValue = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double num2 = Common.CalcuteSimpleMovingAvg(r, (int) base.IndPeriod[0].ParamValue, (int) base.IndPeriod[1].ParamValue, this.tlsSma.Name, base.SourceDic["Close"], curValue, base.Crab.DataSource);
            return new double[] { num2 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsSma };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.tlsSma);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double num = this.Get(r)[0];
                        base.Crab.SetValue(this.TlsSma.Name, num, r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.SourceDic["Close"] = source[0];
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            this.TlsSma = base.Crab.AddLine(fields[0], panelID);
        }

        public LineShape TlsSma
        {
            get
            {
                return this.tlsSma;
            }
            set
            {
                this.tlsSma = value;
            }
        }
    }
}

